package com.coocaa.internship.service.serviceImpl;

import com.coocaa.internship.common.ResultCode;
import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.dao.DeliverCvDao;
import com.coocaa.internship.pojo.Company;
import com.coocaa.internship.pojo.DeliverCv;
import com.coocaa.internship.pojo.PositionInfo;
import com.coocaa.internship.pojo.UserCV;
import com.coocaa.internship.service.DeliverCvService;
import com.coocaa.internship.util.ParamvalidatorUtil;
import com.coocaa.internship.vo.DeliverInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 简历投递业务实现类
 * @Author: zwh
 * @Date: 2019-08-02
 * @Time: 0:38
 */
@Service
public class DeliverCvServiceImpl implements DeliverCvService {

    @Autowired
    DeliverCvDao deliverCvDao;

    @Autowired
    PositionInfoServicelmpl positionInfoServicelmpl;

    @Autowired
    CompanyServiceImpl companyService;

    @Autowired
    UserCvServiceImpl userCvService;

    @Override
    public ResultData insertDeliverCv(DeliverCv deliverCv) {
        ParamvalidatorUtil.paramValidate(deliverCv);
        //是否改简历已投递过
        DeliverCv resultDeliverCv = deliverCvDao.selectDeliverCvByCvIdAndCompanyId(deliverCv);
        //未投递则继续
        if (resultDeliverCv == null) {
            Integer integer = deliverCvDao.insertDeliverCv(deliverCv);
            if (integer > 0) {
                Boolean result = userCvService.changStatusToDelivered(deliverCv.getUserCvId());
                if (result) {
                    return ResultData.success(null);
                }
            }
            return ResultData.failed();
        }
        return ResultData.builder().code(ResultCode.USERCV_REPEATED_DELIVERY.getCode())
                .message(ResultCode.USERCV_REPEATED_DELIVERY.getMessage()).build();
    }

    @Override
    public ResultData selectDeliverCvByCvId(Long userCvId) {
        ParamvalidatorUtil.paramValidate(userCvId);
        ResultData resultData = userCvService.selectUserCvById(userCvId);
        if (resultData.getCode() == ResultCode.SUCCESS.getCode()) {
            List<DeliverCv> deliverCvs = deliverCvDao.selectDeliverCvByCvId(userCvId);
            if (!deliverCvs.isEmpty()) {
                List<DeliverInfoVO> deliverInfoVOS = getDeliverInfoVOS(deliverCvs);
                return ResultData.success(deliverInfoVOS);
            }
            return ResultData.success("该简历未被投递");
        } else {
            return resultData.builder().code(ResultCode.FAILED.getCode())
                    .message("该简历不存在").build();
        }

    }


    @Override
    public ResultData selectDeliverCvByUserId(Long userId) {
        ParamvalidatorUtil.paramValidate(userId);
        //查询所有已经投递出的简历
        List<UserCV> userCVs;
        try {
            userCVs = (List<UserCV>) userCvService.selectDeliveredCvByUserId(userId).getData();
        } catch (ClassCastException e) {
            return ResultData.builder().code(ResultCode.USERCV_NOT_EXISTS.getCode())
                    .message(ResultCode.USERCV_NOT_EXISTS.getMessage()).build();
        }
        if (!userCVs.isEmpty()) {
            //通过每一个简历的id取查询投递到对应岗位的信息和所属公司信息
            //并将该简历的id作为key,查询到的信息可能是多个，即List<DeliverInfoVO>,并将其作为value
            Map<Long, List<DeliverInfoVO>> map = new HashMap<>();
            for (UserCV userCV : userCVs) {
                Long userCvId = userCV.getUserCvId();
                List<DeliverCv> deliverCvs = deliverCvDao.selectDeliverCvByCvId(userCvId);

                List<DeliverInfoVO> deliverInfoVOS = getDeliverInfoVOS(deliverCvs);
                map.put(userCvId, deliverInfoVOS);
            }
            return ResultData.success(map);
        } else {
            return ResultData.success("还未曾投递过简历");
        }
    }

    private List<DeliverInfoVO> getDeliverInfoVOS(List<DeliverCv> deliverCvs) {
        List<DeliverInfoVO> deliverInfoVOS = new ArrayList<>();
        for (DeliverCv deliverCv : deliverCvs) {
            Map positionAndCompany = (Map) positionInfoServicelmpl.selectPositionInfoById(deliverCv.getPositionId()).getData();
            PositionInfo positionInfo = (PositionInfo) positionAndCompany.get("positionInfo");
            Company company = (Company) positionAndCompany.get("company");

            DeliverInfoVO deliverInfoVO = new DeliverInfoVO();
            BeanUtils.copyProperties(company, deliverInfoVO);
            BeanUtils.copyProperties(positionInfo, deliverInfoVO);

            deliverInfoVOS.add(deliverInfoVO);
        }
        return deliverInfoVOS;
    }
    @Override
    public ResultData deleteDeliverCvByCvId(Long userCvId, Long positionId) {
        ParamvalidatorUtil.paramValidate(userCvId, positionId);
        Integer integer = deliverCvDao.deleteDeliverCvByCvId(userCvId, positionId);
        if (integer > 0) {
            return ResultData.success(null);
        }
        return ResultData.failed();
    }
}
